﻿using System;
namespace Moonlite.Services.CodeCompletion
{
    public class AndromedaObject
    {
        public AndromedaObjectDescription Description { get; set; }
        public AndromedaObject Parent { get; set; }
        public int Offset { get; set; }
        public int Length { get; set; }
        private AndromedaObjectCollection children = new AndromedaObjectCollection();
        public AndromedaObjectCollection Children
        {
            get { return children; }
        }

        private AndromedaObjectCollection GetAllChildrenCore(AndromedaObject parent)
        {
            AndromedaObjectCollection children = new AndromedaObjectCollection();
            foreach (AndromedaObject child in parent.Children)
            {
                if (child is IAndromedaObjectWithName)
                {
                    children.Add(child);
                    if (child.Children.Count > 0)
                    {
                        children.AddRange(GetAllChildrenCore(child));
                    }
                }
            }
            return children;
        }

        public virtual AndromedaObjectCollection GetAllChildren()
        {
            AndromedaObjectCollection children = new AndromedaObjectCollection();
            foreach (AndromedaObject @object in Children)
            {
                children.AddRange(GetAllChildrenCore(@object));
                children.Add(@object);
            }
            return children;
        }

        public AndromedaObject()
            : this(null)
        {
        }

        public AndromedaObject(AndromedaObject parent)
        {
            Parent = parent;
        }
    }
}
